<?php

class DateHelper extends AppHelper{

    static $days = array(
        'fr' => array(
            0 => 'Lundi',
            1 => 'Mardi',
            2 => 'Mercredi',
            3 => 'Jeudi',
            4 => 'Vendredi',
            5 => 'Samedi',
            6 => 'Dimanche'
        ),

        'an' => array(
            0 => 'Monday',
            1 => 'Thuesday',
            2 => 'Wednesday',
            3 => 'Thursday',
            4 => 'Friday',
            5 => 'Saturday',
            6 => 'Sunday'
        )
    );
    static $month = array(
        'fr' => array(
            0 => 'Unknow',
            1 => 'Janvier',
            2 => 'Février',
            3 => 'Mars',
            4 => 'Avril',
            5 => 'Mai',
            6 => 'Juin',
            7 => 'Juillet',
            8 => 'Août',
            9 => 'Septembre',
            10 => 'Octobre',
            11 => 'Novembre',
            12 => 'Decembre'
        ),
        'an' => array(
            0 => 'Unknow',
            1 => 'January',
            2 => 'Febuary',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'Juin',
            7 => 'Jully',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December'
        )
    );

     
    static  function getDisplay($d, $options = array()) {
                if (!empty ($d)){
                    $default = array(
                                    'lang' => 'an',
                                    'separator' => ' ',
                                    'format' => 'DMY - HMS',
                                    'style' => '',
                                    'precision' => 'hour'
                    );
                    // On met a dÃ©faut les options non prÃ©cisÃ©
                    foreach ($options as $k => $v){
                                    $default[$k] = $v;
                    }

                    $d = explode('-', $d);
                    if (!empty($d[2])){
                        $hour = explode(' ',$d[2]);
                        $d[2] = $hour[0];
                        if (!empty($hour[1])){
                            $hour = $hour[1];
                            $hour = explode(':',$hour);
                        }
                    }
                    else{

                        str_replace('H', '', $default['precision']);
                        str_replace('M', '', $default['precision']);
                        str_replace('S', '', $default['precision']);
                    }

                    $r = '';

                    switch (strtolower($default['precision'])){
                            case 'second';
                                    $max = 6;
                            break;
                            case 'minute';
                                    $max = 5;
                            break;
                            case 'hour';
                                    $max = 4;
                            break;
                            case 'day';
                                    $max = 3;
                            break;
                            case 'month':
                                    $max = 2;
                            break;
                            case 'year':
                                    $max = 1;
                                    $default['format'] = 'Y';
                            break;
                            default:
                                    $max = strlen($default['format']);
                    }

                    for ($i = 0; $i < $max; $i++){
                            switch ($default['format'][$i]){
                                    case 'D';
                                            $r .= $d[2];
                                    break;
                                    case 'M':
                                            $r .= self::$month[$default['lang']][intval($d[1])];
                                    break;
                                    case 'Y':
                                            $r .= $d[0];
                                    break;
                                    case 'H':
                                            $r .= $hour[0].'h';
                                    break;
                                    case 'I':
                                            $r .= $hour[1];
                                    break;
                                    case 'S':
                                            $r .= $hour[2];
                                    break;
                            }
                            $r .= $default['separator'];
                    }
                    return $r;
                }
                else
                    return '';
	}

     static function interval($d1, $d2=null) {
          $debut = new DateTime($d1);

          if ($d2 == null){
               // Execution de cod
               $fin = new DateTime('now');
          }
          else{
               $fin = new DateTime($d2);
          }

          $interval = $debut->diff($fin);
          return $interval;
    }
    
    
    static function interpreteur($date){
        
        /*
         * Type de date possible
         * 15/01/2001
         * 01/02/11
         * 12 janvier 92
         * 13 mars 1992
         * 2010, Mars, 14
         * April 7 2008 ou April 7th 2008 (Américan)
         * 
         * 
         * 
         */
        
        // 1 : On test si des lettre et chiffre sont ecrite    
        
                
        if (preg_match('/[0-9]{1,} (day[s]?|week[s]?|year[s]?) ago/i', $date)){
            $date = str_replace('ago', '', $date);
            $date = strtotime ( '-'.$date , strtotime ( date('Y-m-d G:i:s') ) ) ;
            $year = date('Y',$date);
            $month = date('m', $date);
            $day = date('d', $date);
        }
        else if (preg_match( '/[a-zA-Z]{1,}[0-9]{1,}/', $date)){
            $dateDecompo = explode(' ', $date);
            if (count($dateDecompo) == 3){
                $day = $dateDecompo[0];
                
                $month = $dateDecompo[1];                
                $allMonths = array_flip(self::$month['fr']);
                
                if (isset($allMonths[$month])){
                    $month = $allMonths[$month];
                }
                else{
                    echo 'A faire ! ';
                }
                
                $year = $dateDecompo[2];
            }
            else{
                echo 'NOT GOOD';
            }
        }
        else if (strlen($date) == 10){
            $day = substr($date, 0, 2);
            $month = substr($date,3, 2);
            $year = substr($date,6, 4);
        }
        // On test si il n'y a que des chiffres 
        else if (preg_match('/^[0-9]*$/', $date)){
            $day = substr($date, 0, 2);
            $month = substr($date,2, 2);
            $year = substr($date,4, 4);            
        }
        // Que des lettre 
        else if (preg_match("/^[a-zA-Z']*$/", $date)){
            if ($date == 'today' || $date == 'aujourd\'hui'){
                $year = date('Y');
                $month = date('m');
                $day = date('d');
            }
            else if ($date == 'yesterday' || $date == 'ier'){
                //$date = date('Y-m-d');    
                extract(self::minus(array(
                    'year' => date('Y'),
                    'month' => date('m'),
                    'day' => date('d')
                    ), 1));               
            }
            else{
                echo "NON RECONNU ! ";
            }
        }
        // Pas que des chiffres : 
        else{
            // Si 8 de long : 
            if (strlen($date) == 8){
                $day = substr($date, 0, 2);
                $month = substr($date,3, 2);
                $year = substr($date,6, 2);                
            }            
        }
        
        if (strlen($year) == 2){
            if ($year > (substr(date('Y'), 2))){
                $year = '19'.$year;
            }
            else{
                $year = '20'.$year;
            }
        }
        
        $dateFormat = $year.'-'.$month.'-'.$day;
        
        if (checkdate($month, $day, $year)){
            return $dateFormat;
        }
        else{            
            return false;
        }
        
    }
    
    static function minus($date, $nb = 1){
        
        
        
        if (is_array($date)){
            extract($date);
        }
        
        if ($day - $nb <= 0){
            if ($month == 1){
                $year -= 1;
                $day = 31;
                $month = 12;
            }
            else{
                $month -= 1;
                $day = 31;
            }
        }
        else{
            $day -= 1;
        }
        
        return array(
            'year' => $year, 
            'month' => $month, 
            'day' => $day
        );
    }
    
    static function isBissextile($year){
        return $year % 400 == 0 || ($year % 4 == 0 && $year % 100 != 100);
    }
    
    
}

?>
